<?php
/*
  功能：文件 model
  作者：cxm
  时间：2019-01-11
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class File extends BaseModel {
//    use SoftDeletes;
//    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='files';  //绑定表

    /**
     * 图片地址
     */
    public function getPathAttribute(){
        if ( !isset($this->attributes['path']) || empty($this->attributes['path'])){
            return '';
        }
        $path = $this->attributes['path'];
        $domain_name = (((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://') . env('DOMAIN_NAMES', $_SERVER['SERVER_NAME']);
        return $domain_name.'/storage/'.$path;
    }

    /**
     * 获取文件路径
     * @param $id 文件ID
     * @return mixed
     */
    static public function getPath($id){
        return self::where('id',$id)->value('path');
    }

    /**
     * 查询图片
     * @param $page 页码
     * @param $limit 每页显示条数
     * @return mixed
     */
    static public function getData($page,$limit){
        $data =  self::orderBy('created_at','desc')
            ->whereIn('extension',config('exam.extension'));
        $count = $data->count();
        $data = $data->skip(($page - 1) * $limit)
            ->take($limit)
            ->get()
            ->toArray();
        return ['total'=>$count,'data'=>$data];
    }

    /**
     * 根据文件ID获取路径
     * @param array $file_ids
     * @return mixed
     */
    static public function getPathData($file_ids=array()){
        return self::whereIn('id',$file_ids)->get(['id','path'])->toArray();
    }
}